﻿/*
 ******************************************************************
 *      
 * 域名空间　	: GreatB2C.DALSQLServer
 * 类　名　称	: Model.cs
 * 类　功　能	: 数据操作实现层
 * 类　流　程    :                             
 * 运用算法　	:                             
 * 编　写　人	: 王超             
 * 编写日期　	: 2011/4/10
 * 最近更新日期　:
 * 版　　本　	: V1.0                           
 * 描　　述　	: Descript of Class                                                 
 *                                        
 ******************************************************************
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using GreatB2C.Model;
using GreatB2C.DALFactory;
using GreatB2C.DALProfile;
using GreatB2C.Config;
using GreatB2C.Utility;
using System.Configuration;
using System.Text;

namespace GreatB2C.DALSQLServer
{
    /// <summary>
    /// 类描述：
    /// </summary>
    [Serializable]
    public class Model : DbBase , IModel
    {
        string strSql = string.Empty;

        #region implement methods

        /// <summary>
        /// 获取获取参数参数列表
        /// </summary>
        /// <param name="ac"></param>
        /// <returns></returns>
        private SqlParameter[] GetParameters( GreatB2C.Model.Model ac )
        {
            int i = 0;
            SqlParameter[] param = new SqlParameter[ 35 ];

            param[ i ] = new SqlParameter( "@ModelID" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.ModelID;

            param[ i ] = new SqlParameter( "@ModelName" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.ModelName;

            param[ i ] = new SqlParameter( "@ModelType" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.ModelType;

            param[ i ] = new SqlParameter( "@Description" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.Description;

            param[ i ] = new SqlParameter( "@TableName" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.TableName;

            param[ i ] = new SqlParameter( "@ItemName" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.ItemName;

            param[ i ] = new SqlParameter( "@ItemUnit" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.ItemUnit;

            param[ i ] = new SqlParameter( "@ItemIcon" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.ItemIcon;

            param[ i ] = new SqlParameter( "@IsCountHits" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.IsCountHits;

            param[ i ] = new SqlParameter( "@Disabled" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.Disabled;

            param[ i ] = new SqlParameter( "@Field" , SqlDbType.NText );
            param[ i++ ].Value = ac.Field;

            param[ i ] = new SqlParameter( "@DefaultTemplateFile" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.DefaultTemplateFile;

            param[ i ] = new SqlParameter( "@EnableCharge" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.EnableCharge;

            param[ i ] = new SqlParameter( "@EnableSignin" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.EnableSignin;

            param[ i ] = new SqlParameter( "@AddInfoFilePath" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.AddInfoFilePath;

            param[ i ] = new SqlParameter( "@ManageInfoFilePath" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.ManageInfoFilePath;

            param[ i ] = new SqlParameter( "@PreviewInfoFilePath" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.PreviewInfoFilePath;

            param[ i ] = new SqlParameter( "@BatchInfoFilePath" , SqlDbType.NVarChar , 200 );
            param[ i++ ].Value = ac.BatchInfoFilePath;

            param[ i ] = new SqlParameter( "@Character" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.Character;

            param[ i ] = new SqlParameter( "@MaxPerUser" , SqlDbType.Int , 4 );
            param[ i++ ].Value = ac.MaxPerUser;

            param[ i ] = new SqlParameter( "@PrintTemplate" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.PrintTemplate;

            param[ i ] = new SqlParameter( "@EnableVote" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.EnableVote;

            param[ i ] = new SqlParameter( "@SearchTemplate" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.SearchTemplate;

            param[ i ] = new SqlParameter( "@AdvanceSearchFormTemplate" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.AdvanceSearchFormTemplate;

            param[ i ] = new SqlParameter( "@AdvanceSearchTemplate" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.AdvanceSearchTemplate;

            param[ i ] = new SqlParameter( "@ChargeTips" , SqlDbType.NVarChar , 4000 );
            param[ i++ ].Value = ac.ChargeTips;

            param[ i ] = new SqlParameter( "@NeedPointChargeTips" , SqlDbType.NVarChar , 4000 );
            param[ i++ ].Value = ac.NeedPointChargeTips;

            param[ i ] = new SqlParameter( "@OutTimeChargeTips" , SqlDbType.NVarChar , 4000 );
            param[ i++ ].Value = ac.OutTimeChargeTips;

            param[ i ] = new SqlParameter( "@UsePointChargeTips" , SqlDbType.NVarChar , 4000 );
            param[ i++ ].Value = ac.UsePointChargeTips;

            param[ i ] = new SqlParameter( "@CommentManageTemplate" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.CommentManageTemplate;

            param[ i ] = new SqlParameter( "@AnonymouseTemplate" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.AnonymouseTemplate;

            param[ i ] = new SqlParameter( "@UserAddContentTemplate" , SqlDbType.NVarChar , 255 );
            param[ i++ ].Value = ac.UserAddContentTemplate;

            param[ i ] = new SqlParameter( "@EnableVerificationCode" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.EnableVerificationCode;

            param[ i ] = new SqlParameter( "@EnableWap" , SqlDbType.Bit , 1 );
            param[ i++ ].Value = ac.EnableWap;

            param[ i ] = new SqlParameter( "@WapField" , SqlDbType.NText );
            param[ i++ ].Value = ac.WapField;


            return param;
        }

        /// <summary>
        /// 检查是否有重复记录。
        /// 注意：这里要手动修改一下，需要加入次主键做为第二条件（主要是为了修改操作）
        /// </summary>
        /// <param name="objModel">对应的数据映射类</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool IsExists( GreatB2C.Model.Model objModel )
        {
            string Sql = "SELECT Count(*) "
                            + " FROM "
                            + "[dbo].[" + Prefix + "Model]"
                            + " WHERE "
                            + "	[ModelID] = @ModelID"
                //+" and 次主键=@次主键"
                            ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int count = Convert.ToInt32( DbHelper.ExecuteScalar( CommandType.Text , Sql , param ).ToString( ) );
                if ( count > 0 )
                {
                    return true;
                }

                return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 检索出表中所有记录	
        /// </summary>
        /// <param name="aryResult">返回对象数据集合</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public DataTable ListModel( )
        {
            DataTable dtData = new DataTable( );
            string Sql = "SELECT "
                            + "[ModelID],"
                            + "[ModelName],"
                            + "[ModelType],"
                            + "[Description],"
                            + "[TableName],"
                            + "[ItemName],"
                            + "[ItemUnit],"
                            + "[ItemIcon],"
                            + "[IsCountHits],"
                            + "[Disabled],"
                            + "[Field],"
                            + "[DefaultTemplateFile],"
                            + "[EnableCharge],"
                            + "[EnableSignin],"
                            + "[AddInfoFilePath],"
                            + "[ManageInfoFilePath],"
                            + "[PreviewInfoFilePath],"
                            + "[BatchInfoFilePath],"
                            + "[Character],"
                            + "[MaxPerUser],"
                            + "[PrintTemplate],"
                            + "[EnableVote],"
                            + "[SearchTemplate],"
                            + "[AdvanceSearchFormTemplate],"
                            + "[AdvanceSearchTemplate],"
                            + "[ChargeTips],"
                            + "[NeedPointChargeTips],"
                            + "[OutTimeChargeTips],"
                            + "[UsePointChargeTips],"
                            + "[CommentManageTemplate],"
                            + "[AnonymouseTemplate],"
                            + "[UserAddContentTemplate],"
                            + "[EnableVerificationCode],"
                            + "[EnableWap],"
                            + "[WapField]"
                            + " FROM "
                            + "[dbo].[" + Prefix + "Model]";

            try
            {
                return DbHelper.ExecuteTable( CommandType.Text , Sql , null );
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }

        /// <summary>
        /// 根据表中主键ID查询记录信息
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>返回对象数据映射</returns>
        public GreatB2C.Model.Model ListByID( GreatB2C.Model.Model objModel )
        {
            string Sql = "SELECT * "
                            + " FROM "
                            + "[dbo].[" + Prefix + "Model]"
                            + " WHERE "
                            + "	[ModelID] = @ModelID";

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                using ( DataTable dtData = DbHelper.ExecuteTable( CommandType.Text , Sql , param ) )
                {
                    if ( dtData.Rows.Count > 0 )
                    {
                        GreatB2C.Model.Model modClass = new GreatB2C.Model.Model( );

                        modClass.ModelID = Convert.ToInt32( dtData.Rows[ 0 ][ "ModelID" ].ToString( ) );
                        modClass.ModelName = dtData.Rows[ 0 ][ "ModelName" ].ToString( );
                        modClass.ModelType = Convert.ToInt32( dtData.Rows[ 0 ][ "ModelType" ].ToString( ) );
                        modClass.Description = dtData.Rows[ 0 ][ "Description" ].ToString( );
                        modClass.TableName = dtData.Rows[ 0 ][ "TableName" ].ToString( );
                        modClass.ItemName = dtData.Rows[ 0 ][ "ItemName" ].ToString( );
                        modClass.ItemUnit = dtData.Rows[ 0 ][ "ItemUnit" ].ToString( );
                        modClass.ItemIcon = dtData.Rows[ 0 ][ "ItemIcon" ].ToString( );
                        modClass.IsCountHits = Convert.ToBoolean( dtData.Rows[ 0 ][ "IsCountHits" ].ToString( ) );
                        modClass.Disabled = Convert.ToBoolean( dtData.Rows[ 0 ][ "Disabled" ].ToString( ) );
                        modClass.Field = dtData.Rows[ 0 ][ "Field" ].ToString( );
                        modClass.DefaultTemplateFile = dtData.Rows[ 0 ][ "DefaultTemplateFile" ].ToString( );
                        modClass.EnableCharge = Convert.ToBoolean( dtData.Rows[ 0 ][ "EnableCharge" ].ToString( ) );
                        modClass.EnableSignin = Convert.ToBoolean( dtData.Rows[ 0 ][ "EnableSignin" ].ToString( ) );
                        modClass.AddInfoFilePath = dtData.Rows[ 0 ][ "AddInfoFilePath" ].ToString( );
                        modClass.ManageInfoFilePath = dtData.Rows[ 0 ][ "ManageInfoFilePath" ].ToString( );
                        modClass.PreviewInfoFilePath = dtData.Rows[ 0 ][ "PreviewInfoFilePath" ].ToString( );
                        modClass.BatchInfoFilePath = dtData.Rows[ 0 ][ "BatchInfoFilePath" ].ToString( );
                        if ( dtData.Rows[ 0 ][ "Character" ] == null || dtData.Rows[ 0 ][ "Character" ].ToString( ).ToString( ) == "" )
                            modClass.Character = 0;
                        else
                            modClass.Character = Convert.ToInt32( dtData.Rows[ 0 ][ "Character" ].ToString( ) );
                        modClass.MaxPerUser = Convert.ToInt32( dtData.Rows[ 0 ][ "MaxPerUser" ].ToString( ) );
                        modClass.PrintTemplate = dtData.Rows[ 0 ][ "PrintTemplate" ].ToString( );
                        modClass.EnableVote = Convert.ToBoolean( dtData.Rows[ 0 ][ "EnableVote" ].ToString( ) );
                        modClass.SearchTemplate = dtData.Rows[ 0 ][ "SearchTemplate" ].ToString( );
                        modClass.AdvanceSearchFormTemplate = dtData.Rows[ 0 ][ "AdvanceSearchFormTemplate" ].ToString( );
                        modClass.AdvanceSearchTemplate = dtData.Rows[ 0 ][ "AdvanceSearchTemplate" ].ToString( );
                        modClass.ChargeTips = dtData.Rows[ 0 ][ "ChargeTips" ].ToString( );
                        modClass.NeedPointChargeTips = dtData.Rows[ 0 ][ "NeedPointChargeTips" ].ToString( );
                        modClass.OutTimeChargeTips = dtData.Rows[ 0 ][ "OutTimeChargeTips" ].ToString( );
                        modClass.UsePointChargeTips = dtData.Rows[ 0 ][ "UsePointChargeTips" ].ToString( );
                        modClass.CommentManageTemplate = dtData.Rows[ 0 ][ "CommentManageTemplate" ].ToString( );
                        modClass.AnonymouseTemplate = dtData.Rows[ 0 ][ "AnonymouseTemplate" ].ToString( );
                        modClass.UserAddContentTemplate = dtData.Rows[ 0 ][ "UserAddContentTemplate" ].ToString( );
                        modClass.EnableVerificationCode = Convert.ToBoolean( dtData.Rows[ 0 ][ "EnableVerificationCode" ].ToString( ) );
                        if ( dtData.Rows[ 0 ][ "EnableWap" ] == null || dtData.Rows[ 0 ][ "EnableWap" ].ToString( ).ToString( ) == "" )
                            modClass.EnableWap = false;
                        else
                            modClass.EnableWap = Convert.ToBoolean( dtData.Rows[ 0 ][ "EnableWap" ].ToString( ) );
                        modClass.WapField = dtData.Rows[ 0 ][ "WapField" ].ToString( );

                        return modClass;
                    }
                }

                return null;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }

        /// <summary>
        /// 新增一条新记录到表中
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Insert( GreatB2C.Model.Model objModel , DataTable dtFields )
        {
            bool retFlg = false;
            SqlTransaction trans = null;
            int sqlVer = 2000;
            Int32.TryParse( ConfigurationManager.AppSettings[ "SQLServerVer" ].ToString( ) , out sqlVer );

            using ( SqlConnection sqlConn = new SqlConnection( DBConfig.CmsConString ) )
            {
                try
                {
                    sqlConn.Open( );
                    trans = sqlConn.BeginTransaction( );

                    strSql = "INSERT INTO "
                        + "[dbo].[" + Prefix + "Model]"
                        + "("
                        + "[ModelName],"
                        + "[ModelType],"
                        + "[Description],"
                        + "[TableName],"
                        + "[ItemName],"
                        + "[ItemUnit],"
                        + "[ItemIcon],"
                        + "[IsCountHits],"
                        + "[Disabled],"
                        + "[Field],"
                        + "[DefaultTemplateFile],"
                        + "[EnableCharge],"
                        + "[EnableSignin],"
                        + "[AddInfoFilePath],"
                        + "[ManageInfoFilePath],"
                        + "[PreviewInfoFilePath],"
                        + "[BatchInfoFilePath],"
                        + "[Character],"
                        + "[MaxPerUser],"
                        + "[PrintTemplate],"
                        + "[EnableVote],"
                        + "[SearchTemplate],"
                        + "[AdvanceSearchFormTemplate],"
                        + "[AdvanceSearchTemplate],"
                        + "[ChargeTips],"
                        + "[NeedPointChargeTips],"
                        + "[OutTimeChargeTips],"
                        + "[UsePointChargeTips],"
                        + "[CommentManageTemplate],"
                        + "[AnonymouseTemplate],"
                        + "[UserAddContentTemplate],"
                        + "[EnableVerificationCode],"
                        + "[EnableWap],"
                        + "[WapField]"
                        + ") VALUES ("
                        + "@ModelName,"
                        + "@ModelType,"
                        + "@Description,"
                        + "@TableName,"
                        + "@ItemName,"
                        + "@ItemUnit,"
                        + "@ItemIcon,"
                        + "@IsCountHits,"
                        + "@Disabled,"
                        + "@Field,"
                        + "@DefaultTemplateFile,"
                        + "@EnableCharge,"
                        + "@EnableSignin,"
                        + "@AddInfoFilePath,"
                        + "@ManageInfoFilePath,"
                        + "@PreviewInfoFilePath,"
                        + "@BatchInfoFilePath,"
                        + "@Character,"
                        + "@MaxPerUser,"
                        + "@PrintTemplate,"
                        + "@EnableVote,"
                        + "@SearchTemplate,"
                        + "@AdvanceSearchFormTemplate,"
                        + "@AdvanceSearchTemplate,"
                        + "@ChargeTips,"
                        + "@NeedPointChargeTips,"
                        + "@OutTimeChargeTips,"
                        + "@UsePointChargeTips,"
                        + "@CommentManageTemplate,"
                        + "@AnonymouseTemplate,"
                        + "@UserAddContentTemplate,"
                        + "@EnableVerificationCode,"
                        + "@EnableWap,"
                        + "@WapField"
                        + ")";

                    SqlParameter[] param = GetParameters( objModel );

                    int affect = DbHelper.ExecuteNonQuery( trans , CommandType.Text , strSql , param );

                    if ( affect == 1 )
                    {
                        #region 创建数据表

                        StringBuilder sbInsert = new StringBuilder( @"CREATE TABLE " + StringHelper.ChkSQL( objModel.TableName ) + @" 
                                (
                                    ID integer NOT NULL PRIMARY KEY " );

                        if ( dtFields.Rows.Count > 0 )
                        {
                            foreach ( DataRow dr in dtFields.Rows )
                            {
                                sbInsert.Append( "," + dr[ "FieldName" ].ToString( ) + DataDefine.GetSqlDataType( dr[ "FieldType" ].ToString( ) , 0 ) );
                            }
                        }

                        sbInsert.Append( ");" );

                        if ( sqlVer <= 2000 )
                        {
                            sbInsert.Append( @" EXEC sp_addextendedproperty  N'MS_Description',  N'自动编号',  N'user',  N'dbo',  N'table',  N'" + StringHelper.ChkSQL( objModel.TableName ) + @"',  N'column',  N'ID'" );
                            
                            foreach ( DataRow dr in dtFields.Rows )
                            {
                                sbInsert.Append( @" EXEC sp_addextendedproperty  N'MS_Description',  N'" + dr[ "FieldAlias" ].ToString( ) + "',  N'user',  N'dbo',  N'table',  N'" + StringHelper.ChkSQL( objModel.TableName ) + @"',  N'column',  N'" + dr[ "FieldName" ].ToString( ) + "'" );
                            }
                        }
                        else if ( sqlVer >= 2005 )
                        {
                            sbInsert.Append( @" EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'自动编号' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'" + StringHelper.ChkSQL( objModel.TableName ) + @"', @level2type=N'COLUMN', @level2name=N'ID'" );
                            
                            foreach ( DataRow dr in dtFields.Rows )
                            {
                                sbInsert.Append( @" EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'" + dr[ "FieldAlias" ].ToString( ) + "' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'" + StringHelper.ChkSQL( objModel.TableName ) + @"', @level2type=N'COLUMN', @level2name=N'" + dr[ "FieldName" ].ToString( ) + "'" );
                            }
                        }

                        DbHelper.ExecuteNonQuery( trans , CommandType.Text , sbInsert.ToString( ) , null );
                        #endregion

                        retFlg = true;

                        trans.Commit( );
                    }
                    else
                    {
                        throw new Exception( "操作失败" );
                    }
                }
                catch ( Exception e )
                {
                    trans.Rollback( );
                    sqlConn.Close( );
                    throw e;
                }
                finally
                {
                    sqlConn.Close( );
                    trans.Dispose( );
                    sqlConn.Dispose( );
                }
            }
            return retFlg;
        }

        /// <summary>
        /// 根据主键ID更新指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Update( GreatB2C.Model.Model objModel )
        {
            string Sql = "UPDATE "
                        + "[dbo].[" + Prefix + "Model]"
                        + " SET "
                        + "[ModelName] = @ModelName,"
                        + "[ModelType] = @ModelType,"
                        + "[Description] = @Description,"
                        + "[TableName] = @TableName,"
                        + "[ItemName] = @ItemName,"
                        + "[ItemUnit] = @ItemUnit,"
                        + "[ItemIcon] = @ItemIcon,"
                        + "[IsCountHits] = @IsCountHits,"
                        + "[Disabled] = @Disabled,"
                        + "[Field] = @Field,"
                        + "[DefaultTemplateFile] = @DefaultTemplateFile,"
                        + "[EnableCharge] = @EnableCharge,"
                        + "[EnableSignin] = @EnableSignin,"
                        + "[AddInfoFilePath] = @AddInfoFilePath,"
                        + "[ManageInfoFilePath] = @ManageInfoFilePath,"
                        + "[PreviewInfoFilePath] = @PreviewInfoFilePath,"
                        + "[BatchInfoFilePath] = @BatchInfoFilePath,"
                        + "[Character] = @Character,"
                        + "[MaxPerUser] = @MaxPerUser,"
                        + "[PrintTemplate] = @PrintTemplate,"
                        + "[EnableVote] = @EnableVote,"
                        + "[SearchTemplate] = @SearchTemplate,"
                        + "[AdvanceSearchFormTemplate] = @AdvanceSearchFormTemplate,"
                        + "[AdvanceSearchTemplate] = @AdvanceSearchTemplate,"
                        + "[ChargeTips] = @ChargeTips,"
                        + "[NeedPointChargeTips] = @NeedPointChargeTips,"
                        + "[OutTimeChargeTips] = @OutTimeChargeTips,"
                        + "[UsePointChargeTips] = @UsePointChargeTips,"
                        + "[CommentManageTemplate] = @CommentManageTemplate,"
                        + "[AnonymouseTemplate] = @AnonymouseTemplate,"
                        + "[UserAddContentTemplate] = @UserAddContentTemplate,"
                        + "[EnableVerificationCode] = @EnableVerificationCode,"
                        + "[EnableWap] = @EnableWap,"
                            + "[WapField] = @WapField"
                        + " WHERE "
                        + "[ModelID] = @ModelID"
                        ;

            try
            {
                SqlParameter[] param = GetParameters( objModel );

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool Delete( GreatB2C.Model.Model objModel )
        {
            bool retFlg = false;
            SqlTransaction trans = null;
            int sqlVer = 2000;
            Int32.TryParse( ConfigurationManager.AppSettings[ "SQLServerVer" ].ToString( ) , out sqlVer );

            using ( SqlConnection sqlConn = new SqlConnection( DBConfig.CmsConString ) )
            {
                try
                {
                    sqlConn.Open( );
                    trans = sqlConn.BeginTransaction( );

                    #region 先删除数据表

                    StringBuilder sbDelete = new StringBuilder( "select TableName from [" + Prefix + "Model]" );
                    sbDelete.Append( " where" );
                    sbDelete.Append( " ModelID = @ModelID" );

                    SqlCommand sqlCommand = new SqlCommand( sbDelete.ToString( ) , sqlConn );

                    sqlCommand.Transaction = trans;
                    SqlParameterCollection sqlParameterCollection = sqlCommand.Parameters;
                    sqlParameterCollection.Add( "@ModelID" , SqlDbType.Int ).Value = objModel.ModelID;

                    string tablename = sqlCommand.ExecuteScalar( ).ToString( );

                    if ( tablename != "" )
                    {
                        sbDelete = new StringBuilder( @"drop table " + tablename );
                        sqlCommand = new SqlCommand( sbDelete.ToString( ) , sqlConn );
                        sqlCommand.Transaction = trans;
                        sqlCommand.ExecuteNonQuery( );
                    }
                    #endregion

                    #region 再删除数据
                    sbDelete.Remove( 0 , sbDelete.Length );
                    sbDelete.Append( "DELETE FROM "
                            + "[dbo].[" + Prefix + "Model]"
                            + " WHERE "
                            + "[ModelID] = @ModelID" );

                    SqlParameter param = new SqlParameter( "@ModelID" , SqlDbType.Int , 4 );
                    param.Value = objModel.ModelID;

                    int affect = DbHelper.ExecuteNonQuery( CommandType.Text , sbDelete.ToString( ) , param );
                    #endregion

                    if ( affect == 1 )
                        retFlg = true;
                    else
                        throw new Exception( "error" );

                    trans.Commit( );
                }
                catch ( Exception e )
                {
                    trans.Rollback( );
                    sqlConn.Close( );
                    throw e;
                }
                finally
                {
                    sqlConn.Close( );
                    trans.Dispose( );
                    sqlConn.Dispose( );
                }
            }

            return retFlg;
        }

        /// <summary>
        /// 根据ID列表批量删除记录
        /// 数据格式：1,2,3,4,5
        /// </summary>
        /// <param name="strList">ID列表</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool DeleteList( string strList )
        {
            string Sql = "DELETE FROM "
                            + "[dbo].[" + Prefix + "Model]"
                            + " WHERE "
                            + "[ModelID] in ('" + strList + "')"
                            ;

            try
            {
                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , null );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据模板类型检索出模板信息列表
        /// </summary>
        /// <param name="modelType">模板类型</param>
        /// <returns>数据集合</returns>
        public DataTable ListModelByType( int modelType )
        {
            DataTable dtData = new DataTable( );
            string Sql = "SELECT * FROM "
                            + "[dbo].[" + Prefix + "Model] where ModelType=@ModelType";

            try
            {
                SqlParameter param = new SqlParameter( "@ModelType" , SqlDbType.Int , 4 );
                param.Value = modelType;

                return DbHelper.ExecuteTable( CommandType.Text , Sql , param );
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return null;
            }
        }

        /// <summary>
        /// 更改模板禁用状态
        /// </summary>
        /// <param name="disabled">是否禁用</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool UpdateModelStatus( int modelID , bool disabled )
        {
            string Sql = "UPDATE "
                        + "[dbo].[" + Prefix + "Model]"
                        + " SET "
                        + "[Disabled] = @Disabled"
                        + " WHERE "
                        + "[ModelID] = @ModelID"
                        ;

            try
            {
                SqlParameter[] param = new SqlParameter[ 2 ];

                param[ 0 ] = new SqlParameter( "@ModelID" , SqlDbType.Int , 4 );
                param[ 0 ].Value = modelID;

                param[ 1 ] = new SqlParameter( "@Disabled" , SqlDbType.Bit );
                param[ 1 ].Value = disabled;

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        /// <summary>
        /// 根据主键ID更新字段信息
        /// </summary>
        /// <param name="modelID">模板编号</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool UpdateModelFieldInfo( int modelID , string fields )
        {
            string Sql = "UPDATE "
                        + "[dbo].[" + Prefix + "Model]"
                        + " SET "
                        + "[Field] = @Field"
                        + " WHERE "
                        + "[ModelID] = @ModelID"
                        ;

            try
            {
                SqlParameter[] param = new SqlParameter[ 2 ];

                param[ 0 ] = new SqlParameter( "@ModelID" , SqlDbType.Int , 4 );
                param[ 0 ].Value = modelID;

                param[ 1 ] = new SqlParameter( "@Field" , SqlDbType.NText );
                param[ 1 ].Value = fields;

                int affect = DbHelper.ExecuteNonQuery( CommandType.Text , Sql , param );

                if ( affect == 1 )
                    return true;
                else
                    return false;
            }
            catch ( Exception ex )
            {
                Console.Write( ex.Message );
                return false;
            }
        }

        #endregion
    }
}
